package com.easidea.tim.action.util;

import com.easidea.tim.action.base.BaseAction;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.webwork.interceptor.ServletResponseAware;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlDealAction extends BaseAction implements ServletResponseAware,ServletRequestAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -9161106589569255074L;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public void XmlDeal(){
		this.xmlUpdateDemo();
		String url = this.returnUrl();
		PrintWriter jumpout = null;
		if(url.equals("0")){
			return;
		}else{
			try {
				jumpout = response.getWriter();
				response.setContentType("text/html;charset=GBK");
			} catch (IOException e) {
				
			} finally {
				if (jumpout != null) {
					jumpout.print("<script>");
					jumpout.print("location.href = '"+url+"'");
					jumpout.print("</script>");
					jumpout.flush();
				}
			}
		}
	}
	
	public void backToSystem(){
		this.backToXml();
		PrintWriter jumpout = null;
		try {
			jumpout = response.getWriter();
			response.setContentType("text/html;charset=GBK");
		} catch (IOException e) {
			
		} finally {
			if (jumpout != null) {
				jumpout.print("操作成功！已切换回正式系统");
				jumpout.flush();
			}
		}
	}
	
	public void redirect(){
		String url = this.returnUrl();
		PrintWriter jumpout = null;
		if(url.equals("0")){
			return;
		}else{
			try {
				jumpout = response.getWriter();
				response.setContentType("text/html;charset=GBK");
			} catch (IOException e) {
				
			} finally {
				if (jumpout != null) {
					jumpout.print("<script>");
					jumpout.print("location.href = '"+url+"'");
					jumpout.print("</script>");
					jumpout.flush();
				}
			}
		}
	}

	public static boolean doc2XmlFile(Document document, String filename) {
		boolean flag = true;
		try {
			/** 将document中的内容写入文件中 */
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer();
			/** 编码 */
			// transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
			DOMSource source = new DOMSource(document);
			StreamResult result = new StreamResult(new File(filename));
			transformer.transform(source, result);
		} catch (Exception ex) {
			flag = false;
			ex.printStackTrace();
		}
		return flag;
	}

	public static Document load(String filename) {
		Document document = null;
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			document = builder.parse(new File(filename));
			document.normalize();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return document;
	}

	/**
	 * 演示修改文件的具体某个节点的值
	 */
	public  void xmlUpdateDemo() {
		Document document = load("d://change.xml");
		Node root = document.getDocumentElement();
		/** 如果root有子元素 */
		if (root.hasChildNodes()) {
			/** ftpnodes */
			NodeList ftpnodes = root.getChildNodes();
			/** 循环取得ftp所有节点 */
			for (int i = 0; i < ftpnodes.getLength(); i++) {
				NodeList ftplist = ftpnodes.item(i).getChildNodes();
				for (int k = 0; k < ftplist.getLength(); k++) {
					Node subnode = ftplist.item(k);
					/** 删除ftp-chn节点 */
					// if
					// (subnode.getNodeType()==Node.ELEMENT_NODE&&subnode.getNodeName()=="ftp-chn")
					// {
					// ftpnodes.item(i).removeChild(subnode);
					// }
					/** 修改ftp-host的值为 192.168.0.1 */
				//	System.out.println(subnode.getNodeName());
					if (subnode.getNodeName() == "change") {
						subnode.getFirstChild().setNodeValue("1");
					}
				}

			}
		}

		doc2XmlFile(document, "d://change.xml");
	}
	
	public  void backToXml() {
		Document document = load("d://change.xml");
		Node root = document.getDocumentElement();
		/** 如果root有子元素 */
		if (root.hasChildNodes()) {
			/** ftpnodes */
			NodeList ftpnodes = root.getChildNodes();
			/** 循环取得ftp所有节点 */
			for (int i = 0; i < ftpnodes.getLength(); i++) {
				NodeList ftplist = ftpnodes.item(i).getChildNodes();
				for (int k = 0; k < ftplist.getLength(); k++) {
					Node subnode = ftplist.item(k);
					/** 删除ftp-chn节点 */
					// if
					// (subnode.getNodeType()==Node.ELEMENT_NODE&&subnode.getNodeName()=="ftp-chn")
					// {
					// ftpnodes.item(i).removeChild(subnode);
					// }
					/** 修改ftp-host的值为 192.168.0.1 */
				//	System.out.println(subnode.getNodeName());
					if (subnode.getNodeName() == "change") {
						subnode.getFirstChild().setNodeValue("0");
					}
				}

			}
		}

		doc2XmlFile(document, "d://change.xml");
	}
	
	public String returnUrl() {
		String result="0";
		String flag = "0";
		Document document = load("d://change.xml");
		Node root = document.getDocumentElement();
		/** 如果root有子元素 */
		if (root.hasChildNodes()) {
			/** ftpnodes */
			NodeList ftpnodes = root.getChildNodes();
			/** 循环取得ftp所有节点 */
			for (int i = 0; i < ftpnodes.getLength(); i++) {
				NodeList ftplist = ftpnodes.item(i).getChildNodes();
				for (int k = 0; k < ftplist.getLength(); k++) {
					Node subnode = ftplist.item(k);
					if (subnode.getNodeName() == "change") {
						flag = subnode.getFirstChild().getNodeValue();
					}
					if(flag.equals("1")){
						if (subnode.getNodeName() == "url2") {
							result = subnode.getFirstChild().getNodeValue();
						}
					}
				}

			}
		}
		System.out.println("----------------------" + result);
		return result;
	}

}
